package com.tbit.uqbike.webmanager.controller;

import cn.hutool.core.util.StrUtil;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.uqbike.constant.MapConstant;
import com.tbit.uqbike.constant.UserConstant;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.HotIntelligentSite;
import com.tbit.uqbike.object.pojo.ParkPoint;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.base.CalibrateService;
import com.tbit.uqbike.service.base.ParkPointService;
import com.tbit.uqbike.service.business.HotIntelligentSiteService;
import com.tbit.uqbike.webmanager.util.Assert;
import com.tbit.uqbike.webmanager.util.StringUtil;
import com.tbit.utils.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author ：hzw
 * @date ：Created in 2021/8/27 13:32
 * @description：热点站点审核控制层
 * @modified By：
 * @version: $
 */
@RestController
@RequestMapping(value = "/hotIntelligentSite")
public class HotIntelligentSiteController extends BaseController {
    @Autowired
    private HotIntelligentSiteService hotIntelligentSiteService;
    @Autowired
    private ParkPointService parkPointService;
    @Autowired
    private CalibrateService calibrateService;

    public static void main(String[] args) {
        String str = "100,2000;1261,163165;42452,51252";
        List<String> num = StringUtil.getList(str);
        System.out.println(num);
    }

    @RequestMapping(value = "/queryPage")
    public JsonResult queryPage(@RequestParam(defaultValue = "10") Integer rowCount,
                                @RequestParam(defaultValue = "1") Integer pageNO,
                                Integer state, Long startTime, Long endTime, Integer accountId) {

        List<HotIntelligentSite> HotIntelligentSiteList = hotIntelligentSiteService.queryPage(startTime, endTime, state, pageNO, rowCount, accountId);

        for (HotIntelligentSite site : HotIntelligentSiteList) {
            List<String> num = StringUtil.getList(site.getPoints());
            site.setReturnNum(num.size());
        }

        Integer count = hotIntelligentSiteService.queryCount(startTime, endTime, state, accountId);

        return JsonResult.ok(HotIntelligentSiteList, count);
    }

    /* 处理 */
    @RequestMapping("/handle")
    @OperateLog(module = ModuleEnum.HOTINTELLIGENTSITE, methods = MethodEnum.HOTINTELLIGENTSITE_HANDLE)
    public JsonResult handle(@NotNull(message = "ver.siteId.notNull") Integer siteId,
                             String auditReason, Integer agree, String remark, Integer accountId,
                             String name, Integer mapType, String points, Integer allowRange, Integer capacity,
                             HttpServletRequest request) {

        LoginInfo loginInfo = getLogin(request);
        if (loginInfo.type.equals(UserConstant.USER_AGENT)) {
            accountId = loginInfo.id;
        } else {
            Assert.notNull(accountId, "ver.accountId .notNull");
        }
        if (StrUtil.isNotEmpty(name) && name.length() > 100){
            throw new BaseException("站点名称过长");
        }else if (StrUtil.isNotEmpty(auditReason) && auditReason.length() > 50){
            throw new BaseException("审核理由过长");
        }else if (StrUtil.isNotEmpty(remark) && remark.length() > 50){
            throw new BaseException("审核备注过长");
        }

        HotIntelligentSite hotIntelligentSite = new HotIntelligentSite();
        hotIntelligentSite.setSiteId(siteId);
        hotIntelligentSite.setAuditReason(auditReason);
        hotIntelligentSite.setAuditTime(DateTimeUtil.getNowTime());
        hotIntelligentSite.setRemark(remark);
        hotIntelligentSite.setState(1);
        hotIntelligentSiteService.handle(hotIntelligentSite);
        /**如果有代理商Id，直接自动添加停车点，误差范围默认100米*/
        if (accountId != null && agree == 1) {
            hotIntelligentSite = hotIntelligentSiteService.getHotIntelligentSite(siteId);
            /**经纬度校准*/
            ParkPoint parkPoint = new ParkPoint();
            parkPoint.setAccountId(accountId);
            parkPoint.setName(name);
            parkPoint.setParkPointType(1);
            parkPoint.setLo(hotIntelligentSite.getLoC());
            parkPoint.setLa(hotIntelligentSite.getLaC());
            parkPoint.setAllowRange(allowRange);
            parkPoint.setCapacity(capacity);
            parkPoint.setPoints(points);
            parkPoint.setRemark("用户申请审核通过自动创建站点");
            calibrateService.calParkPoint(parkPoint, mapType, MapConstant.MAP_ORI);
            parkPointService.insert(parkPoint, loginInfo);
        }
        return JsonResult.ok();

    }
}
